<template>
  <div style="margin-top:20px">
    <!-- ---------------------------------- iec104 ------------------------------------------->
    <el-table ref="multipleTable" height="250" border :data="rule_map" size='mini'>
      <template slot="empty">
        <el-empty :description="$t('暂无数据')"></el-empty>
      </template>
      <el-table-column prop="rule_id" align='center' :label="$t('规则ID')" width="80" />
      <el-table-column prop="proto" align='center' width="120" :label="$t('协议名称')" />
      <el-table-column prop="type" align='center' width="80" :label="$t('数据类型')">
        <template slot-scope="scope">
          {{ $t(typeList[scope.row.type]) }}
        </template>
      </el-table-column>
      <el-table-column prop="action" align='center' width="80" :label="$t('动作')">
        <template slot-scope="scope">
          {{ $t(actionList[scope.row.action]) }}
        </template>
      </el-table-column>
      <el-table-column prop="func" align='center' :formatter="(row, column, cellValue) => funcFormatter(row, column, cellValue, 'func')" width="140" :label="$t('功能码')" />
      <el-table-column prop="transfer_reason" align='center' width="140" :label="$t('传送原因')" />
      <el-table-column prop="information_object_address_start" align='center' :formatter="informationObjectAddressRangeFormatter" width="140" :label="$t('信息对象地址范围')" />
      <el-table-column prop="common_address_start" align='center' :formatter="commonAddressRangeFormatter" width="140" :label="$t('公共地址范围')" />
      <el-table-column prop="register_value_range" align='center' :formatter="registerValueRangeFormatter" width="140" :label="$t('寄存器值范围')" />
      <el-table-column prop="accept_range" align='center' width="100" :label="$t('匹配范围')">
        <template slot-scope="scope">
          {{ $t(acceptRangeList[scope.row.accept_range]) }}
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('操作')" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="success" plain @click="editRule(scope.row)" size='mini' :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" plain @click="deleteRule(scope.row)" size='mini' :class="'del' +(scope.$index+1)">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- ---------------------------------- iec104_connect ------------------------------------------->
    <el-table ref="multipleTable" height="250" border :data="rule_map_connect" size='mini' style="margin-top:20px;">
      <template slot="empty">
        <el-empty :description="$t('暂无数据')"></el-empty>
      </template>
      <el-table-column prop="rule_id" align='center' :label="$t('规则ID')" width="80" />
      <el-table-column prop="proto" align='center' width="120" :label="$t('协议名称')" />
      <el-table-column prop="operate_type" align='center' width="120" :label="$t('类型')">
        <template slot-scope="scope">
          {{ $t(operateTypeList[scope.row.operate_type]) }}
        </template>
      </el-table-column>
      <el-table-column prop="action" align='center' width="80" :label="$t('动作')">
        <template slot-scope="scope">
          {{ $t(actionList[scope.row.action]) }}
        </template>
      </el-table-column>
      <el-table-column prop="relate_func" align='center' :formatter="(row, column, cellValue) => funcFormatter(row, column, cellValue, 'relate_func')" width="140" :label="$t('写操作功能码')" />
      <el-table-column prop="relate_transfer_reason" align='center' width="140" :label="$t('写操作传送原因')" />
      <el-table-column prop="relate_information_object_address" align='center' width="140" :label="$t('写操作信息体地址')" />
      <el-table-column prop="relate_common_address" align='center' width="140" :label="$t('写操作公共地址')" />

      <el-table-column prop="be_relate_func" align='center' :formatter="(row, column, cellValue) => funcFormatter(row, column, cellValue, 'be_relate_func')" width="140" :label="$t('关联写操作功能码')" />
      <el-table-column prop="be_relate_transfer_reason" align='center' width="140" :label="$t('关联写操作传送原因')" />
      <el-table-column prop="be_relate_information_object_address" align='center' width="155" :label="$t('关联写操作信息对象地址')" />
      <el-table-column prop="be_relate_common_address" align='center' width="140" :label="$t('关联写操作公共地址')" />
      <el-table-column prop="be_relate_register_value_range" align='center' width="145" :formatter="relateRegisterValueRangeFormatter" :label="$t('关联信息体元素值范围')" />
      <el-table-column align="center" :label="$t('操作')" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="success" plain @click="editRule(scope.row)" size='mini'>{{$t('编辑')}}</el-button>
          <el-button type="danger" plain @click="deleteRule(scope.row)" size='mini'>{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import ruleArray from '../../ruleArray'
export default {
  name: 'Iec104Table',
  props: {
    ruleMap: {
      type: Array,
      default () {
        return []
      }
    }
  },
  watch: {
    ruleMap: {
      handler (newValue, oldValue) {
        this.rule_map = []
        this.rule_map_connect = []
        newValue.forEach(val => {
          if (val.proto === 'iec104') {
            this.rule_map.push(val)
          } else if (val.proto === 'iec104_connect') {
            this.rule_map_connect.push(val)
          }
        })
      },
      deep: true // 深度监听
    }
  },
  data () {
    return {
      dialogVisible: false,
      ruleDialogVisible: false,
      rule_map: [],
      rule_map_connect: [],
      funcList: ruleArray.iecFuncShowList,
      typeList: ruleArray.iesTypeList,
      operateTypeList: { '0': '深度过滤', '1': '关联操作' },
      showSelectList: ['45', '46', '47', '58', '59', '60'],
      registerValueRangeSelectList: {
        '0': '关',
        '1': '开',
        '2': '开关'
      },
      actionList: { 'accept': '允许', 'alert': '告警' },
      acceptRangeList: {
        'all': '所有',
        'single': '单个'
      }
    }
  },
  mounted () {
    this.rule_map = []
    this.rule_map_connect = []
    this.ruleMap.forEach(val => {
      if (val.proto === 'iec104') {
        this.rule_map.push(val)
      } else if (val.proto === 'iec104_connect') {
        this.rule_map_connect.push(val)
      }
    })
  },
  methods: {
    deleteRule (row) {
      this.$emit('delRuleMap', row)
    },
    editRule (row) {
      this.$emit('largeConfig', row)
    },
    funcFormatter (row, column, cellValue, item) {
      return this.$t(this.funcList[row[item]]) || row[item]
    },
    informationObjectAddressRangeFormatter (data) {
      if (data.type === '0') {
        return data.information_object_address_start + '-' + data.information_object_address_end
      }
    },
    commonAddressRangeFormatter (data) {
      if (data.type === '0') {
        return data.common_address_start + '-' + data.common_address_end
      }
    },
    registerValueRangeFormatter (data) {
      if (data.type === '0') {
        if (this.showSelectList.includes(data.func)) {
          return this.$t(this.registerValueRangeSelectList[data.register_value_range.split('-')[0]])
        } else {
          return data.register_value_range
        }
      }
    },
    relateRegisterValueRangeFormatter (row) {
      if (this.showSelectList.includes(row.be_relate_func)) {
        return this.$t(this.registerValueRangeSelectList[row.be_relate_register_value_range.split('-')[0]])
      } else {
        return row.be_relate_register_value_range
      }
    }
  }
}
</script>
